<template>
	<view class="wrap">
		<u-card class="task-list-item" :show-head="false" :border="true" padding="36" :head-border-bottom="false">
			<view slot="body">
				<u-row gutter="26" style="gap:24rpx">
					<u-col span="12">
						<view class="apply-text"><span>保养单号：</span>{{this.maintain_order_number}}</view>
					</u-col>
					<u-col span="12">
						<view class="apply-text"><span>保养时间：</span>{{this.maintain_date}}</view>
					</u-col>
					<u-col span="12">
						<view class="apply-text"><span>保养人：</span>{{this.nice_name}}</view>
					</u-col>
				</u-row>
			</view>
		</u-card>

		<view class="">
			<view class="head-text">保养工单</view>
			<u-card class="task-list-item" :show-head="false" :border="true" padding="36" margin="24rpx"
				:head-border-bottom="false">
				<view slot="body">
					<u-row gutter="26" style="gap:24rpx">
						<u-col span="12">

							<u-radio-group v-model="allValue" @change="radioGroupChange1">
								<u-radio shape="square" @change="radioChange1(item.allValue)"
									v-for="(item, index) in list" :key="index" :name="item.allValue"
									:label="item.allValue" :disabled="item.disabled">
									{{ item.name }}
								</u-radio>
							</u-radio-group>



							<view v-for="(item, index) in equipment_infos" :key="item.id" style="margin-bottom: 48rpx;">
								<view class="container-checked">
									<view>{{ item.process_name }}-{{ item.equipment_name }}</view>
									<u-radio-group v-model="item.checkedValue"
										@change="radioGroupChange(item.checkedList)">
										<u-radio @change="radioChange(checkedItem.value)" label-size="16"
											v-for="(checkedItem, checkedIndex) in item.checkedList" :key="checkedIndex"
											:name="checkedItem.value" :label="checkedItem.value"
											:disabled="checkedItem.disabled">
											{{ checkedItem.name }}
										</u-radio>
									</u-radio-group>
								</view>

								<view class="container-tips">
									<view>备注</view>
									<u-input v-model="item.remark" type="text" :border="true" />
								</view>
							</view>


						</u-col>
					</u-row>
				</view>
			</u-card>

			<view class="edit-up">
				<u-button @click="orderEdit()" size="medium" type="primary">提交</u-button>
			</view>
		</view>

	</view>
</template>
<script>
	export default {
		data() {
			return {
				id: "",
				maintain_order_number: "",
				maintain_date: "",
				nice_name: "",
				equipment_infos: [{
					equipment_id: "169267377219940",
					equipment_name: "2号污水提升泵",
					is_maintain: 1,
					process_name: "进水",
					remark: "",
				}],
				list: [{
						name: '全选',
						disabled: false,
						allValue: 1
					},
					{
						name: '全不选',
						disabled: false,
						allValue: 0
					},
				],
				allValue: "",
				value: ''
			}
		},
		onLoad(options) {
			this.id = options.id
			this.orderView(this.id)
			this.getOrderName()
		},
		created() {
			this.updateAllCheckedState(); // 初始化全选按钮的状态
		},
		methods: {
			//全选
			radioGroupChange1(val) {
				if (val == 1) {
					this.equipment_infos.forEach(item => {
						item.checkedValue = 1;
						item.is_maintain = item.checkedValue
						item.checkedList.forEach(checkedItem => {
							checkedItem.checked = true;
						});
					});
				} else if (val == 0) {
					this.equipment_infos.forEach(item => {
						item.checkedValue = 0;
						item.is_maintain = item.checkedValue
						item.checkedList.forEach(checkedItem => {
							checkedItem.checked = false;
						});
					});
				}
				this.updateAllCheckedState();
			},

			radioChange1(value) {
				if (this.equipment_infos.every(item => item.checkedValue === 1)) {
					this.allChecked = true;
				} else if (this.equipment_infos.every(item => item.checkedValue === 0)) {
					this.allChecked = false;
				} else {
					this.updateAllCheckedState();
				}
			},

			radioChange(e) {
				console.log(e, 'radioChange');
			},

			radioGroupChange(e) {
				console.log(e, "radioGroupChange");
				this.equipment_infos.forEach(item => {
					item.is_maintain = item.checkedValue;
				});
				this.updateAllCheckedState();
			},
			navTo(url) {
				uni.navigateTo({
					url: url
				});
			},
			updateAllCheckedState() {
				this.list = [{
						name: '全选',
						disabled: false,
						allValue: 1
					},
					{
						name: '全不选',
						disabled: false,
						allValue: 0
					},
				];
				this.allChecked = false;
			},
			//查看
			orderView(id) {
				this.$u.api.orderView({
					"algorithmProcessId": "water_maintain_order_view",
					"param": {
						"data": {
							id: id,
						}
					}
				}).then(res => {
					this.nice_name = res.data.data.result.nice_name
					this.maintain_order_number = res.data.data.result.maintain_order_number
					this.maintain_date = res.data.data.result.maintain_date
					this.equipment_infos = JSON.parse(res.data.data.result.equipment_infos)
					this.equipment_infos.forEach(item => {
						item.checkedList = [{
								name: '已保养',
								disabled: false,
								value: 1,
							},
							{
								name: '未保养',
								disabled: false,
								value: 0,
							}
						];
					});
					// console.log(this.equipment_infos, 'this.equipment_infos2');
				}).catch(error => {
					this.$u.toast('查询异常');
					console.error(error);
				});
			},
			//获取名字
			getOrderName() {
				this.$u.api.orderView({
					"algorithmProcessId": "getOrderName",
					"param": {
						"data": {
							user_uuid: uni.getStorageSync('uuid'),
						}
					}
				}).then(res => {
					this.nice_name = res.data.data.result[0].nice_name
				}).catch(error => {
					this.$u.toast('查询异常');
					console.error(error);
				});
			},
			//编辑
			orderEdit() {
				console.log(this.equipment_infos, 'Edit');
				this.$u.api.orderEdit({
					"algorithmProcessId": "water_maintain_order_update",
					"param": {
						"data": {
							id: this.id,
							maintain_user: uni.getStorageSync('uuid'),
							equipment_infos: JSON.stringify(this.equipment_infos),
						}
					}
				}).then(res => {
					uni.showToast({
						title: '提交成功',
						icon: 'success',
						duration: 1000
					})

					setTimeout(() => {
						uni.switchTab({
							url: "/pages/sys/book/index",
						});
					}, 600);
					
					
				}).catch(error => {
					this.$u.toast('查询异常');
					console.error(error);
				});
			}
		},
	};
</script>
<style lang="scss" scoped>
	@import '../../../common/uni.css';
	@import 'style/upkeepEdit.css';
</style>